<?php


/**
 * Notes:
 * PROJECT_NAME : ERP v1.8 重构erp
 * User: 夏鱼儿<397072174@qq.com>
 * Date: 2024/5/16
 * Time: 9:58
 */

namespace app\service\order;

use app\company\model\CompanyGoodsRule;
use app\company\model\CompanyOrderStorage;
use app\company\model\WarehouseStorageGoodsLogs;
use Exception;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class Stock extends Base
{
    /**
     * 更新订单库存 (该操作目前是在发货时执行)
     * @return void
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function update(): void
    {
        $this->order->storeOrderDetails()->select()->each(function ($item) {
            $this->decCompanyOrderStorage($item->goods_rule_id, $item->quantity, $item->id);
        });
    }

    /**
     * @param $goods_rule_id
     * @param $quantity
     * @param $detail_id
     * @return void
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @throws Exception
     */
    public function decCompanyOrderStorage($goods_rule_id, $quantity, $detail_id): void
    {
//        $storage = CompanyOrderStorage::goodsRuleId($goods_rule_id)->gs($this->order->company_id)->order('production_time')->select();
        // 因为他的写法，现在需要过滤没库存的数据
        $storage = CompanyOrderStorage::goodsRuleId($goods_rule_id)->gs($this->order->company_id)->where('quantity','>',0)->order('production_time')->select();
        foreach ($storage as $item) {
            if (!$quantity) break;
            if ($item->quantity <= $quantity) {
                $quantity = $quantity - $item->quantity;
                $storageQuantity = $item->quantity;
                // $item->inc('quantity', -$item->quantity)->update();//更新库存
                $item->inc('quantity', -$item->quantity)->save();//更新库存
                //记录销售情况
//                $this->addWarehouseStorageGoodsLogs($item, $item->quantity, $detail_id);
                // 这个模型和变量要是拿不准就命名一个变量扔进去好不好，你这模型值都变了啊，存的全都乱套了
                $this->addWarehouseStorageGoodsLogs($item, $storageQuantity, $detail_id);
                CompanyOrderStorage::destroy($item->id);//删除该条数据(软删除)
            } else {
                // $item->inc('quantity', -$quantity)->update();//更新库存
                $item->inc('quantity', -$quantity)->save();//更新库存
                $this->addWarehouseStorageGoodsLogs($item, $quantity, $detail_id);   //记录销售情况
                $quantity = 0;
            }
        }
        if ($quantity > 0) exception('库存不足');
    }

    /**
     * 添加记录表 此表能够查询出销售对应的采购订单
     * @param $item
     * @param $quantity
     * @param $detail_id
     * @return void
     */
    public function addWarehouseStorageGoodsLogs($item, $quantity, $detail_id): void
    {
        $money  = CompanyGoodsRule::gs($item->company_id)->goodsRuleId($item->goods_rule_id)->value('money');//得到售价
        $amount = $quantity * $money;
        $data   = [
            'store_order_id'           => $this->order->id,
            'store_order_detail_id'    => $detail_id,
            'company_order_storage_id' => $item->id,
            'warehouse_storage_id'     => $item->warehouse_storage_id,
            'warehouse_storage_name'   => $item->warehouse_storage_name,
            'goods_id'                 => $item->goods_id,
            'goods_rule_id'            => $item->goods_rule_id,
            'goods_cate_id'            => $item->goods_cate_id,
            'quantity'                 => $quantity,
            'type'                     => 2,
            'supplier_id'              => $item->supplier_id,
            'company_id'               => $item->company_id,
            'amount'                   => $amount,
            'production_time'          => $item->production_time,
            'expire_time'              => $item->expire_time,
        ];
        //记录经销商商品销量
        if ($item->supplier_id) {
            $xsGoodsKey     = xsGoodsKeySupplier($item->supplier_id);
            $xsGoodsRuleKey = xsGoodsRuleKeySupplier($item->supplier_id);
            redis()->zIncrBy($xsGoodsRuleKey, $item['quantity'], $item['goods_rule_id']);//记录销售数量+
            redis()->zIncrBy($xsGoodsKey, $item['quantity'], $item['goods_id']);//记录销售数量+
        }
        WarehouseStorageGoodsLogs::create($data);
    }
}